// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', function() {
    // 自动隐藏提示消息
    const alerts = document.querySelectorAll('.alert');
    alerts.forEach(function(alert) {
        setTimeout(function() {
            const bsAlert = new bootstrap.Alert(alert);
            bsAlert.close();
        }, 5000);
    });
    
    // 图片预览功能
    const imageInputs = document.querySelectorAll('input[type="url"]');
    imageInputs.forEach(function(input) {
        input.addEventListener('input', function() {
            const preview = this.parentElement.querySelector('.image-preview');
            if (preview) {
                if (this.value) {
                    preview.src = this.value;
                    preview.style.display = 'block';
                } else {
                    preview.style.display = 'none';
                }
            }
        });
    });
    
    // 平滑滚动
    document.querySelectorAll('a[href^="#"]').forEach(anchor => {
        anchor.addEventListener('click', function (e) {
            e.preventDefault();
            const target = document.querySelector(this.getAttribute('href'));
            if (target) {
                target.scrollIntoView({
                    behavior: 'smooth',
                    block: 'start'
                });
            }
        });
    });
    
    // 动态加载更多内容
    let page = 1;
    const loadMoreBtn = document.getElementById('loadMoreBtn');
    if (loadMoreBtn) {
        loadMoreBtn.addEventListener('click', function() {
            page++;
            fetch(`/api/posts?page=${page}`)
                .then(response => response.json())
                .then(data => {
                    if (data.posts.length > 0) {
                        const postsContainer = document.querySelector('.posts-container');
                        data.posts.forEach(post => {
                            const postElement = createPostElement(post);
                            postsContainer.appendChild(postElement);
                        });
                    } else {
                        loadMoreBtn.style.display = 'none';
                    }
                })
                .catch(error => console.error('Error:', error));
        });
    }
});

// 创建动态元素
function createPostElement(post) {
    const div = document.createElement('div');
    div.className = 'card mb-4 post-card';
    div.innerHTML = `
        <div class="card-body">
            <div class="user-info">
                <img src="${post.user.avatar_url}" alt="${post.user.nickname}">
                <div>
                    <h5 class="card-title mb-0">${post.user.nickname}</h5>
                    <small class="text-muted">${new Date(post.created_at).toLocaleString()}</small>
                </div>
            </div>
            <div class="post-content">${post.content}</div>
            ${post.image_url ? `<img src="${post.image_url}" class="post-image" alt="动态图片">` : ''}
        </div>
    `;
    return div;
}

// 表单验证
function validateForm(form) {
    const requiredFields = form.querySelectorAll('[required]');
    let isValid = true;
    
    requiredFields.forEach(field => {
        if (!field.value.trim()) {
            isValid = false;
            field.classList.add('is-invalid');
        } else {
            field.classList.remove('is-invalid');
        }
    });
    
    return isValid;
}

// 图片上传预览
function previewImage(input) {
    if (input.files && input.files[0]) {
        const reader = new FileReader();
        reader.onload = function(e) {
            const preview = document.querySelector('.image-preview');
            if (preview) {
                preview.src = e.target.result;
                preview.style.display = 'block';
            }
        };
        reader.readAsDataURL(input.files[0]);
    }
} 